<?php


namespace App\Http\Repositories\Services\Impls;


use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Repositories\Daos\IProductSiteDao;
use App\Http\Repositories\Eloquent\ProductSite;
use App\Http\Repositories\Services\IProductSite;
use App\Http\Requests\Admin\ProductSiteInputDTO;

class ProductSiteService implements IProductSite
{
    protected $productSiteDao;

    public function __construct(IProductSiteDao $productSiteDao)
    {
        $this->productSiteDao = $productSiteDao;
    }

    public function byId($id)
    {
        $dao = $this->productSiteDao->find($id);
        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_find_data'));
        }
        return $dao;
    }

    public function lists($search, $page, $size)
    {
        return $this->productSiteDao->lists($search, $page, $size);
    }

    public function store(ProductSiteInputDTO $inputDTO)
    {
        $dao = new ProductSite();
        $dao->name = trim($inputDTO->get('name'));
        $dao->cover = trim($inputDTO->get('cover'));
        $dao->amount = round($inputDTO->get('amount'),2 );
        $dao->save();
    }

    public function update(ProductSiteInputDTO $inputDTO, $id)
    {
        $dao = $this->byId($id);
        $dao->name = trim($inputDTO->get('name'));
        $dao->cover = trim($inputDTO->get('cover'));
        $dao->amount = round($inputDTO->get('amount'),2 );
        $dao->save();
    }

    public function delete($id)
    {
        $dao = $this->byId($id);
        $dao->is_deleted = 1;
        $dao->save();
    }

    public function getAll()
    {
        return $this->productSiteDao->byKey('is_deleted','=','0');
    }
}